<?php
/**
 * Creater: 李青林
 * Date Time : 2018/8/31 14:14
 */

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\DescribeRule;
use Auth;
use DB;
use Illuminate\Http\Request;
use function Sodium\crypto_box_publickey_from_secretkey;
use Illuminate\Support\MessageBag;

class DescribeRuleController extends Controller
{
    use ModelForm;
    protected $headerStr = '积分规则';
    protected $t = 1; //(1积分描述,2秒杀描述,3用户审核(平推),4用户审核(爆款))
    protected $types = [
        1 => '积分规则',
        2 => '秒杀规则',
        3 => '平推拒绝原因',
        4 => '爆款拒绝原因',
        5 => '秒杀拒绝原因',
    ];

    public function index()
    {

        $type = request('type');
        if ($type == 'integral') {
            $this->t = 1;
        } elseif ($type == 'kill_rule') {
            $this->t = 2;
        }elseif ($type == 'goods') {
            $this->t = 3;
        }elseif ($type == 'bursting') {
            $this->t = 4;
        }elseif ($type == 'kill') {
            $this->t = 5;
        }else {
            $this->t = 1;
        }
        $this->headerStr = $this->types[$this->t];
        return Admin::content(function (Content $content) {
            $content->header('日志');
            $content->description($this->headerStr );
            $content->body($this->grid());
        });

    }

    protected function form()
    {
        return Admin::form(DescribeRule::class, function (Form $form) {
            $form->display('id', 'ID:');
            $form->hidden('type', 'type:')->default($this->t);
            if ($this->t == 1) {
                $form->text('content', '扣分详情:');
                $form->number('num', '扣分:')->max(100000000);
            } elseif ($this->t == 2) {
                $form->text('content', '秒杀规则:');
            } else {
                $form->text('content', '失败原因:');
                //$form->hidden('type', 'type:')->default($this->t);
                // $options = ['3' => '平推', '4' => '爆款', '5' => '秒杀'];
            }
            $form->disableReset();
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                //$tools->disableView(); // 去掉跳转列表按钮
            });
        });
    }


    protected function grid()
    {
        return Admin::grid(DescribeRule::class, function (Grid $grid) {
            if ($this->t == 3) {
                $grid->model()->whereIn('type', [3, 4, 5]);
            } else {
                $grid->model()->whereIn('type', [$this->t]);
            }
            $grid->model()->where(['type' => $this->t]);
            if ($this->t == 1) {
                $grid->content('积分扣分内容');
                $grid->num('扣分数量');
            }elseif ($this->t == 2){
                $grid->content('秒杀规则');
            } elseif ($this->t >= 3) {
                $grid->content('商品失败原因');
                $grid->type('商品类型')->display(function ($type) {
                    $types = [
                        3 => '平推',
                        4 => '爆款',
                        5 => '秒杀',
                    ];
                    return $type >= 3 && $type <= 5 ? $types[$type] : '';
                });
            }
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->like('content', '规则内容');
            });
            //$grid->disableCreateButton();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->actions(function ($actions) {
                //$actions->disableDelete();
                //$actions->disableView();
                if (method_exists($actions, 'disableView')) {
                    $actions->disableView();
                }
            });
            $grid->paginate(env('Admin_Limit'));
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        $this->get_type($_SERVER["HTTP_REFERER"]);
        return Admin::content(function (Content $content) use ($id) {
            $content->header('日志');
            $content->description($this->headerStr );
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        $this->get_type($_SERVER["HTTP_REFERER"]);
        return Admin::content(function (Content $content) {
            $content->header('日志');
            $content->description($this->headerStr );
            $content->body($this->form());
        });
    }

    protected function get_type($http_hreffer)
    {
        preg_match_all("/(?<=(\?|&)type=)\w+/i", $http_hreffer, $match);
        if (empty($match[0]) || !isset($match[0][0])) {
            $this->t = 1;
        } else {
            $type = $match[0][0];
            if ($type == 'integral') {
                $this->t = 1;
            } elseif ($type == 'kill_rule') {
                $this->t = 2;
            }elseif ($type == 'goods') {
                $this->t = 3;
            }elseif ($type == 'bursting') {
                $this->t = 4;
            }elseif ($type == 'kill') {
                $this->t =  5;
            }else {
                $this->t = 1;
            }
        }
        $this->headerStr = $this->types[$this->t] . '审核规则';
    }

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('日志');
            $content->description($this->headerStr );
            $content->body($this->form()->edit($id));
        });
    }
}